package com.jzoffer2;

import java.util.Arrays;

import static com.jzoffer.AdjustArrayOrder.swap;

/**
 * TODO
 *
 * @author lefly
 * @version 1.0
 * @classname No40
 * @description
 * @date 2021/2/1 下午1:30
 */
public class No40 {
    public static void main(String[] args) {
        int[] arr = {3,2,1};
        int k = 2;
        int[] res = getLeastNumbers(arr,k);
        System.out.println(Arrays.toString(res));
    }

    public static int[] getLeastNumbers(int[] arr, int k) {
        if (arr == null || arr.length < 1 || k == 0){
            return new int[0];
        }
        int len = arr.length;
        // 冒泡排序
        for (int i = 0; i < k; i++) {
            for (int j = len - 1; j > i ; j--) {
                if(arr[j - 1] > arr[j]){
                    swap(arr,j - 1,j);
                }
            }
        }
        return Arrays.copyOfRange(arr,0,k);
    }

}
